package com.bhs.sansonglogistics.dialog;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.VideoView;

import androidx.annotation.NonNull;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bumptech.glide.Glide;

public class VideoDialog extends Dialog {

    private VideoView mVideo;
    private String url;
    private ImageView mIvPlay;
    private ImageView mProgressBar;

    public VideoDialog(@NonNull Context context, String url) {
        super(context, R.style.MyBottomDialogStyle);
        this.url = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_vide);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT; //设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        initView();
    }
    RotateAnimation ta;
    private void initView() {
        mVideo = findViewById(R.id.video);
        mIvPlay = findViewById(R.id.iv_play);
        mProgressBar = findViewById(R.id.ProgressBar);

        mProgressBar.post(new Runnable() {
            @Override
            public void run() {
                ta= new RotateAnimation(0, 360,mProgressBar.getWidth() / 2, mProgressBar.getHeight() / 2);
                // 设置动画播放的时间
                ta.setDuration(2000);
                ta.setRepeatCount(10);
                // 开始播放动画
                mProgressBar.startAnimation(ta);
            }
        });

        Uri uri = Uri.parse(url);
        //设置视频路径
        mVideo.setVideoURI(uri);
        mVideo.start();
        mVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mVideo.isPlaying()) {
                    mVideo.pause();
                    mIvPlay.setVisibility(View.VISIBLE);
                } else {
                    mVideo.start();
                    mIvPlay.setVisibility(View.GONE);
                }
            }
        });

        mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                ta.cancel();
                mProgressBar.setVisibility(View.GONE);
            }
        });


    }
}
